<?php

namespace App\Admin\Controllers;

use App\Models\Cts;
use App\Models\CtsMonthData;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class CtsMonthDataController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('旅行社月报');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('新增');
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(CtsMonthData::class, function (Grid $grid) {
            $grid->actions(function ($actions) {
                $actions->disableView();
            });
            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                $filter->equal('cts_id','旅行社')->select(Cts::all()->pluck('title', 'id'));
            });
            $grid->disableExport();
            $grid->id('ID')->sortable();
            $grid->cts_id('旅行社')->display(function ($val){
                return Cts::find($val)->title;
            });
            $grid->turnover('营业额');
            $grid->teams_num('接团数量');
            $grid->person_num('接团人数');
            $grid->date('时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(CtsMonthData::class, function (Form $form) {
            $form->select('cts_id', '旅行社')->options(Cts::pluck('title','id'));
            $form->text('turnover','营业额');
            $form->number('teams_num','接团数量');
            $form->number('person_num','接团人数');
            $form->month('date','时间');
        });
    }
}
